#pragma once
#include<iostream>
#include<vector>
#include<functional>
using namespace std;
namespace zgx
{
    template <class T, class Container = vector<T>, class Compare = less<T> >
    class priority_queue
    {
    public:
        priority_queue()
        {}
        template <class InputIterator>
        priority_queue(InputIterator first, InputIterator last)
        {
            while (first != last)
            {
                push(*first);
                ++first;
            }
        }
        void AdjustUp(int child)
        {
            int parent = (child - 1) / 2;
            while (child > 0)
            {
                if (comp(c[parent], c[child]))
                {
                    swap(c[parent], c[child]);
                    child = parent;
                    parent = (child - 1) / 2;
                }
                else
                {
                    break;
                }
            }
        }
        void AdjustDown(int parent)
        {
            int child = parent * 2 + 1;
            while (child < size())
            {
                if (child + 1 < size() && comp(c[child], c[child + 1]))
                {
                    child++;
                }
                if (comp(c[parent], c[child]))
                {
                    swap(c[parent], c[child]);
                    parent = child;
                    child = parent * 2 + 1;
                }
                else
                {
                    break;
                }
            }
        }
        bool empty() const
        {
            return c.empty();
        }
        size_t size() const
        {
            return c.size();
        }
        T& top() const
        {
            return c.front();
        }
        void push(const T& x)
        {
            c.push_back(x);
            AdjustUp(size() - 1);
        }
        void pop()
        {
            swap(c.front(), c.back());
            c.pop_back();
            AdjustDown(0);
        }
    private:
        Container c;
        Compare comp;
    };
};